#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#define PORT 34875
void main(void)
{
	struct sockaddr_in serv_sock, client_sock;
	int lfd = socket(AF_INET, SOCK_STREAM, 0);
	char buf[64], str[64];
	int n, cfd, i, len = sizeof(client_sock);
	
	memset(&serv_sock, 0, sizeof(struct sockaddr_in));
	serv_sock.sin_family = AF_INET;
	serv_sock.sin_addr.s_addr = htonl(INADDR_ANY);
	serv_sock.sin_port = htons(PORT);
	bind(lfd, (struct sockaddr *)&serv_sock, sizeof(struct sockaddr_in));
	if (listen(lfd, 8) < 0) {
		perror(strerror(errno));
		return;
	}
	printf("Accepting connections ...\n");
	while (1) {
		cfd = accept(lfd, (struct sockaddr *)&client_sock, &len);
		inet_ntop(AF_INET, &client_sock.sin_addr, str, sizeof(str)),
		printf("accept connection from %s:%d\n", str,client_sock.sin_port);
		n = read(cfd, buf, sizeof(buf));
		for (i = 0; i < n; i++)
			buf[i] = toupper(buf[i]);
		write(cfd, buf, sizeof(buf));
		close(cfd);
	}
}
